package pl.ptemich.pong.states;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import pl.ptemich.pong.model.FloatPoint;
import pl.ptemich.pong.model.Paddle;
import pl.ptemich.pong.model.ScreenTools;

public class GameState extends AbstractState {

  private Paddle player1Paddle;
  private Paddle player2Paddle;
  
  private FloatPoint boardOrigin;
  
  private static final float BOARD_WIDTH = 500f;
  private static final float BOARD_HEIGHT = 500f;
  private static final float BOARD_DISTANCE = 10f;
  
  @Override
  public int getID() {    
    return 1623;
  }
  
  @Override
  public void init(GameContainer arg0, StateBasedGame arg1)
      throws SlickException {
    player1Paddle = new Paddle(new FloatPoint(BOARD_DISTANCE, Paddle.PADDLE_HEIGHT / 2 + BOARD_DISTANCE), Color.red);
    player2Paddle = new Paddle(new FloatPoint(BOARD_WIDTH - BOARD_DISTANCE, BOARD_HEIGHT - Paddle.PADDLE_HEIGHT / 2 - BOARD_DISTANCE), Color.blue);
  }
  
  @Override
  public void render(GameContainer container, StateBasedGame game, Graphics g)
      throws SlickException {    
    g.setColor(Color.white);
    g.fillRect(boardOrigin.getPosX(), boardOrigin.getPosY(), BOARD_WIDTH, BOARD_HEIGHT);
    
    player1Paddle.draw(g, boardOrigin);
    player2Paddle.draw(g, boardOrigin);
  }
  
  @Override
  public void update(GameContainer container, StateBasedGame game, int delta)
      throws SlickException {

    Input input = container.getInput();

    if (input.isKeyDown(Input.KEY_ESCAPE)) {
      container.exit();
//      game.enterState(StateTools.getStateInstance(MenuState.class).getID(), 
//                      new FadeOutTransition(Color.black), 
//                      new FadeInTransition(Color.black));
    }
    
  }
  
  public GameState(ScreenTools screenTools) {
    super(screenTools);
    boardOrigin = new FloatPoint((screenTools.getScreenWidth() - BOARD_WIDTH) / 2, 
                                  (screenTools.getScreenHeight() - BOARD_HEIGHT) / 2);
  }
  
}
